<?php
	namespace Home\Controller;
	use Think\Controller;	
class IntegralGoodsController extends Controller {
	public function index(){
		$this->display();
	}
    public function goalstore(){
//		//实现数据分页效果
//		//$goods=new \Model\GoodsModel();				
//  	$goods=new \Model\IntegralGoodsModel();
//		//获取数据总条数
//		$total=$goods->count();
//		$per=2;
//		//实例化分页类对象
//		$page_obj=new \Tools\Page($total,$per);
//		$sql="select * from integralgoods order by id desc ".$page_obj->limit;
//		$info=$goods->query($sql);
//		//$info=$goods->limit('7')->order('goods_id desc')->select();
//		//获得页码列表
//		//dump($info);
//		$pagelist=$page_obj->fpage();
//		$this->assign('pagelist',$pagelist);
//		$this->assign('info',$info);
		//获得轮播图信息
		$slides=D('slides')->where("status=1 and pos='indexslides'")->order('sort desc')->select();
//		var_dump($slides);
		//获取最新的商品信息
		$goods=D('integralgoods')->order("salecount desc")->limit(10)->select();
//		var_dump($goods);
		if(session('id')){
		//若用户没有登录则不获取用户的收藏信息，登录则获取	
		//获取该用户收藏的该分类下的所有商品id
		$sql="select f.goods_uid from goods_fav as f,integralgoods as g where f.goods_uid=g.id "." and f.user_uid=".session('id');
		$fav_ids=$favs=D('goods_fav')->query($sql);
		$idstr=array();
		foreach($fav_ids as $k=>$v){
//			$idstr.=$v['goods_uid'].',';
			$idstr[$k]=$v['goods_uid'];
		}
//		var_dump($idstr);exit;
		$this->assign('idstr',$idstr);
		}
		$this->assign('goods',$goods);
		$this->assign('slides',$slides);
		$this->assign('goods',$goods);
		$this->display();
    }
//	public function goodsdetail($id){
//		//查找商品所有信息
//		$goods=new \Model\IntegralGoodsModel();
//		$good_info=$goods->where("id='$id'")->find();
//		//查找热门榜商品信息
//		$hotgoods_info=$goods->field('id,name,price,picture,salecount')->where("id!='$id'")->limit(3)->order('salecount desc')->select();
//		//var_dump($hotgoods_info);
//		$this->assign('good_info',$good_info);
//		$this->assign('hotgoods_info',$hotgoods_info);
//		$this->display();
//	}
	public function findgoods(){
		$name=$_POST['keyword'];	
		$goods=new \Model\IntegralGoodsModel();
		$sql="select * from integralgoods where name like '%$name%' order by salecount desc";
		//echo $sql;
		$find_info=$goods->query($sql);
		//var_dump($info);
		//实例化分页类对象
		$page_obj=new \Tools\Page(count($find_info),9);
		$pagelist=$page_obj->fpage();
		$this->assign('pagelist',$pagelist);
		$this->assign('find_info',$find_info);
		$this->display();
	}
	public function cats(){//展示分类模块,最多三级分类
		$pcat=D('goods_cats')->where("parent_uid=0")->select();
//		var_dump($pcat);
		$ids=array();//储存所有一级分类id
		foreach($pcat as $k=>$v){
			$ids[$k]=$v['uid'];
		}
//		var_dump($ids);
		//echo implode(",",$ids);
		$scat=D('goods_cats')->where("parent_uid in (".implode(",",$ids).")")->select();
//		var_dump($scat);
		$idsfortwo=array();//储存所有二级分类id
			foreach($scat as $k=>$v){
			$idsfortwo[$k]=$v['uid'];
		}
		$sscat=D('goods_cats')->where("parent_uid in (".implode(",",$idsfortwo).")")->select();//查询所有三级分类
//		var_dump($sscat);
		$this->assign("pcat",$pcat);
		$this->assign("scat",$scat);
		$this->assign("sscat",$sscat);
		$this->display();
	}
	//商品列表
	function goodslist($cat_uid,$cat_title){
//		echo $cat_uid;
		$goods=D('integralgoods')->where('cat_uid='.$cat_uid)->select();
//		var_dump($goods);
		if(session('id')){
		//若用户没有登录则不获取用户的收藏信息，登录则获取	
		//获取该用户收藏的该分类下的所有商品id
		$sql="select f.goods_uid from goods_fav as f,integralgoods as g where f.goods_uid=g.id and g.cat_uid=".$cat_uid." and f.user_uid=".session('id');
		$fav_ids=$favs=D('goods_fav')->query($sql);
		$idstr=array();
		foreach($fav_ids as $k=>$v){
//			$idstr.=$v['goods_uid'].',';
			$idstr[$k]=$v['goods_uid'];
		}
//		var_dump($idstr);exit;
		$this->assign('idstr',$idstr);
		}
		$this->assign('cat_title',$cat_title);
		$this->assign('goods',$goods);
		$this->display();
	}
	//收藏商品
	function goodsfav($id){
		$user_uid=session('id');
		$create_time=$_SERVER['REQUEST_TIME'];
		if(D('goods_fav')->where("user_uid=".$user_uid." and goods_uid=".$id)->find()){//已经收藏，则取消收藏,并减少商品收藏数
			if(D('goods_fav')->where("user_uid=".$user_uid." and goods_uid=".$id)->delete()&&D('goods_fav')->execute("update integralgoods set fav_cnt=fav_cnt-1 where id=".$id))
				$this->ajaxReturn(0);
		}else{//没有收藏，则添加收藏记录
			$data=array("user_uid"=>$user_uid,"goods_uid"=>$id,"create_time"=>$create_time);
			if(D('goods_fav')->add($data)&&D('goods_fav')->execute("update integralgoods set fav_cnt=fav_cnt+1 where id=".$id))
			$this->ajaxReturn(1);
		}
	}
	//加入购物车
	function addshopcart($id){
		
		$user_uid=session('id');
		$create_time=$_SERVER['REQUEST_TIME'];
		if(D('goods_cart')->where("user_uid=".$user_uid." and goods_uid=".$id)->find()){//已经存在于购物车，则添加数量
			if(D('goods_cart')->execute("update goods_cart set quantity=quantity+1 where user_uid=".$user_uid." and goods_uid=".$id))
				$this->ajaxReturn(1);
		}else{//购物车不存在该商品，则添加进购物车
			$data=array("user_uid"=>$user_uid,"goods_uid"=>$id,"create_time"=>$create_time,"quantity"=>1);
			if(D('goods_cart')->add($data))
			$this->ajaxReturn(1);
		}
	}
	//商品详情
	function goodsdetail($id){
		$good=D('integralgoods')->where("id=".$id)->find();
		//获取商品最新的三条评论
		$sql="select c.*,u.name as user_name,u.touxiang as touxiang from goods_comment as c,user as u where c.goods_uid={$id} and c.user_uid=u.id and c.status=1 order by c.create_time desc limit 3";
		$comments=D('goods_comment')->query($sql);
//		var_dump($comments);
		$this->assign("comments",$comments);
		$this->assign("good",$good);
		$this->display();
	}
	//搜索商品
	function search($keyword){
//		var_dump($_POST);
		//分页相关
		$total=D('integralgoods')->where("name like '%{$keyword}%'")->count();
		$per=2;
		//实例化分页类对象
		$page_obj=new \Tools\CssPage($total,$per);
		
		//获取符合搜索条件的商品信息
		$sql="select * from integralgoods where name like '%{$keyword}%' order by salecount desc ".$page_obj->limit;
		$goods=D('integralgoods')->query($sql);
//		var_dump($goods);
		if(session('id')){
		//若用户没有登录则不获取用户的收藏信息，登录则获取	
		//获取该用户收藏的该分类下的所有商品id
		$sql="select f.goods_uid from goods_fav as f,integralgoods as g where f.goods_uid=g.id "." and f.user_uid=".session('id');
		$fav_ids=$favs=D('goods_fav')->query($sql);
		$idstr=array();
		foreach($fav_ids as $k=>$v){
			$idstr[$k]=$v['goods_uid'];
		}
			$this->assign('idstr',$idstr);
		}
		$pagelist=$page_obj->fpage(array(3,4,5,6,7,8));
		$this->assign('pagelist',$pagelist);
		$this->assign('total',$total);
		$this->assign('goods',$goods);
		$this->display();
	}
}